package com.fuego.creditsystemcommodity.controller;

import com.fuego.creditsystemcommodity.service.IGoodService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.dto.Result;
import org.example.entity.Good;
import org.example.entity.GoodInfo;
import org.springframework.web.bind.annotation.*;




@RestController
@RequestMapping("/good")
public class GoodController {

    @Resource
    private IGoodService goodService;

    @PostMapping
    public Result addGood(@RequestBody Good good) {
        goodService.save(good);
        return Result.ok(good.getId());
    }


    @PostMapping("goodInfo")
    public Result addGoodInfo(@RequestBody GoodInfo goodInfo) {
        goodService.addGoodInfo(goodInfo);
        return Result.ok(goodInfo.getGoodId());
    }


    @GetMapping("/list/{shopId}")
    public Result queryGoodOfShop(@PathVariable("shopId") Long shopId) {
        return goodService.queryGoodOfShop(shopId);
    }

    @GetMapping("/goodInfo/{goodId}")
    public Result queryGoodById(@PathVariable("goodId") Long goodId) {

        return goodService.queryGoodById(goodId);
    }

    @GetMapping("/search/{goodName}")
    public Result searchGood(@PathVariable("goodName") String goodName) {

        return goodService.searchGood(goodName);
    }

    @PostMapping("/click")
    public Result clickGood(@RequestParam("goodId") Long goodId,
                            @RequestParam("userId") Long userId) {
        return goodService.clickGood(goodId,userId);
    }

    @GetMapping("/list/allGood")
    public Result queryAllGood() {
        return goodService.queryAllGood();
    }


    @GetMapping("/list/pageGood")
    public Result pageGood(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize,
            HttpServletRequest request) {

        request.getSession().setMaxInactiveInterval(3600);

        return goodService.pageGood(page, pageSize, request);
    }
}
